package bysj.controller.basic;

import bysj.domain.ApplicationForSupervisor;
import bysj.domain.ApplicationForSupervisorEntry;
import bysj.domain.Student;
import bysj.domain.Teacher;
import bysj.service.ApplicationForSupervisorService;
import bysj.service.StudentService;
import bysj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

//https://gitee.com/hou-guangxin/bysj.git
@Slf4j
@WebServlet("/applicationForSupervisor.ctl")
public class ApplicationForSupervisorController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        //将JSON字串解析为对象
        JSONObject applicationForSupervisorList_jsonArray =
                req_jsonObj.getJSONObject("applicationForSupervisor");
        System.out.println(applicationForSupervisorList_jsonArray);
        //前端传来的ApplicationForSupervisorList
        ApplicationForSupervisor applicationForSupervisor =
                applicationForSupervisorList_jsonArray.toJavaObject(ApplicationForSupervisor.class);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加对象
        try {
            int student_id = Integer.parseInt(applicationForSupervisorList_jsonArray.get("student_id").toString());
            Student student = StudentService.getInstance().find(student_id);
            applicationForSupervisor.setStudent(student);
            JSONArray applicationForSupervisorEntries_jsonArray = applicationForSupervisorList_jsonArray.getJSONArray("applicationForSupervisorEntries");
            Collection<ApplicationForSupervisorEntry> applicationForSupervisorEntries = applicationForSupervisorEntries_jsonArray.toJavaList(ApplicationForSupervisorEntry.class);
            applicationForSupervisor.setApplicationForSupervisorEntries(applicationForSupervisorEntries);
            int i = 0;
            for(ApplicationForSupervisorEntry applicationForSupervisorEntry: applicationForSupervisorEntries){
                JSONObject jsonObject = applicationForSupervisorEntries_jsonArray.getJSONObject(i);
                int teacher_id = Integer.parseInt(jsonObject.get("teacher_id").toString());
                Teacher teacher = TeacherService.getInstance().find(teacher_id);
                applicationForSupervisorEntry.setTeacher(teacher);
                i++;
            }
        ApplicationForSupervisorService.getInstance().add(applicationForSupervisor);
            message.put("message", "增加成功");
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            log.error(e.getMessage());
        }
        catch(Exception e){
            message.put("message", "网络异常");
            e.printStackTrace();
            log.error(e.getMessage());
        }
        //响应message到前端
        response.getWriter().println(message);
    }
}
